<?php
/**
 * created by zzy
 * date: 2018/1/26 14:07
 */


namespace App\Api\Service\Bill\Controllers;


use App\Eloquent\Ygt\BillTemplate;
use App\Eloquent\Ygt\DepartmentUser;
use App\Engine\Func;
use Framework\BaseClass\Api\Controller;

class TemplateController extends Controller
{
    public $userId          = 25;
    public function index(){}
    public function lists()
    {
        $userId             = Func::getHeaderValueByName('userid',$this->userId);
        $userInfo           = DepartmentUser::getCurrentInfo($userId);
        $type               = request('type',0);
        $page               = request( 'page', 1 );
        $limit              = request('limit', 10 );
        $offset             = ($page - 1) * $limit;
        if( !$userInfo )
        {
            xThrow( ERR_PARAMETER );
        }
        $companyId          = $userInfo->company_id;
        $limit              = $offset = '';
        $templateList       = BillTemplate::getListDetailByCompanyId($companyId,$limit,$offset);
        $templateListArr    = [];
        foreach($templateList as $key=>$val)
        {
            $temp                = $val;
            $imgPath             = Func::getImgUrlById($val['img_id']);
            $temp['img_path']   = $imgPath;
            $templateListArr[$val['type']][]    = $temp;
        }
        $typeList           = BillTemplate::getBillTypeList();
        foreach($typeList as $key=>$val)
        {
            $valId              = $val['id'];
            $temData            = [];
            if(isset($templateListArr[$valId]))
            {
                $temData        = $templateListArr[$valId];
            }
            $typeList[$key]['data']     = $temData;
        }
        return $typeList;
    }
    public function edit()
    {
        $userId             = Func::getHeaderValueByName('userid',$this->userId);
        $userInfo           = DepartmentUser::getCurrentInfo($userId);
        if( !$userInfo )
        {
            xThrow( ERR_PARAMETER );
        }
        $companyId              = $userInfo->company_id;
        $data                   = request()->all();
        $data['company_id']   = $companyId;
        $fieldList              = BillTemplate::$tableFieldList;
        $tableData              = BillTemplate::setDataByTableField($data,$fieldList);
        $tableDataCheckResult   = BillTemplate::checkDataByTableField($tableData,$fieldList);
        if($tableDataCheckResult !== true)
        {
            xThrow(ERR_PARAMETER, $tableDataCheckResult);
        }
        $editResult             = BillTemplate::editData($tableData);
        if($editResult===false)
        {
            xThrow(ERR_UNKNOWN);
        }
        $result         = ['message'=>'编辑成功'];
        return $result;
    }
    public function detail()
    {
        $userId             = Func::getHeaderValueByName('userid',$this->userId);
        $userInfo           = DepartmentUser::getCurrentInfo($userId);
        $id                 = request('id',0);
        if(!$userInfo || !$id)
        {
            xThrow( ERR_PARAMETER );
        }
        $companyId              = $userInfo->company_id;
        $where                  = ['company_id'=>$companyId,'id'=>$id];
        $info                   = BillTemplate::getInfoDetail($companyId,$id);
        if(!$info)
        {
            xThrow(ERR_UNKNOWN);
        }
        $result         = $info->toArray();
        return $result;
    }
    public function del()
    {
        $userId             = Func::getHeaderValueByName('userid',$this->userId);
        $userInfo           = DepartmentUser::getCurrentInfo($userId);
        $id                 = request('id',0);
        if(!$userInfo || !$id)
        {
            xThrow( ERR_PARAMETER );
        }
        $companyId              = $userInfo->company_id;
        $where                  = ['company_id'=>$companyId,'id'=>$id];
        $delResult              = BillTemplate::del($where);
        if(!$delResult)
        {
            xThrow(ERR_UNKNOWN);
        }
        return ['message'=>'删除成功'];
    }
}